package com.yyjj.yyjj.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyjj.yyjj.common.exception.YYJJException;
import com.yyjj.yyjj.common.result.Result;
import com.yyjj.yyjj.common.result.ResultCodeEnum;
import com.yyjj.yyjj.enums.PaymentStatus;
import com.yyjj.yyjj.enums.PaymentType;
import com.yyjj.yyjj.model.order.OrderInfo;
import com.yyjj.yyjj.model.order.PaymentInfo;
import com.yyjj.yyjj.mq.constant.MqConst;
import com.yyjj.yyjj.mq.service.RabbitService;
import com.yyjj.yyjj.order.client.OrderFeignClient;
import com.yyjj.yyjj.payment.mapper.PaymentInfoMapper;
import com.yyjj.yyjj.payment.service.PaymentInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private RabbitService rabbitService;

    @Override
    public PaymentInfo getPaymentInfoByOrderNo(String orderNo) {
       return baseMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo,orderNo));
    }

    @Override
    public PaymentInfo savePaymentInfo(String orderNo) {
        //远程调用查询订单信息
        OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderNo);
        if(orderInfo==null){
            throw new YYJJException(ResultCodeEnum.DATA_ERROR);
        }
        PaymentInfo paymentInfo=new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(PaymentType.WEIXIN);
        paymentInfo.setUserId(orderInfo.getUserId());
        paymentInfo.setOrderNo(orderInfo.getOrderNo());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID);
        String subject = "test";
        paymentInfo.setSubject(subject);
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
//        paymentInfo.setTotalAmount(new BigDecimal("0.01"));
        baseMapper.insert(paymentInfo);
        return paymentInfo;
    }


    //更改订单状态，处理支付结果
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void paySuccess(String orderNo, Map<String, String> resultMap) {
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOrderNo, orderNo);
        PaymentInfo paymentInfo = baseMapper.selectOne(queryWrapper);
        if (paymentInfo.getPaymentStatus() != PaymentStatus.UNPAID) {
            return;
        }
        paymentInfo.setPaymentStatus(PaymentStatus.PAID);
        paymentInfo.setTradeNo(resultMap.get("transaction_id"));
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(resultMap.toString());
        baseMapper.updateById(paymentInfo);
        // 表示交易成功！
        //发送消息
        rabbitService.sendMessage(MqConst.EXCHANGE_PAY_DIRECT, MqConst.ROUTING_PAY_SUCCESS, orderNo);
    }


}
